package com.example.tt_bells_v02;

import java.util.ArrayList;
import java.util.List;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class BellManager extends Service {

	private Handler mHandler = new Handler();
	private static final int ONE_MINUTE = 60000;
	//TODO: maybe incrementing getter for debug_id, or DataBase realization
	int debug_id = 1;
	
	//Bell bs1;
	//Bell bs2;
	List<Bell> bellHolder = new ArrayList<Bell>();
	List<BellTemplate> templateHolder = new ArrayList<BellTemplate>();
	MyBinder binder = new MyBinder();
	
	public void onCreate() {
		super.onCreate();
		Log.d(Tag.TAG, "onCreate");
		
		 NotificationCompat.Builder builder =  
		            new NotificationCompat.Builder(this)  
		            .setSmallIcon(R.drawable.bell_48px)  
		            .setContentTitle("Notifications Example")  
		            .setContentText("This is a test notification");
		
		Intent notificationIntent = new Intent(this, ActivityMain.class);  
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,   
		            PendingIntent.FLAG_UPDATE_CURRENT);  
		builder.setContentIntent(contentIntent);  
		    
			 
		Notification ntf = builder.build();
		ntf.flags |= Notification.FLAG_FOREGROUND_SERVICE;    
		 
		//Notification ntf = new Notification(R.drawable.bell_48px,"Bell Service",System.currentTimeMillis());
		//ntf.flags |= Notification.FLAG_FOREGROUND_SERVICE;
		startForeground(1234, ntf);
		
		mHandler.postDelayed(periodicTask, ONE_MINUTE); 
		
		//bs1 = new Bell(getBaseContext());
		//bs2 = new Bell(getBaseContext());
				
		//Fill holders; TODO: from DataBase
		BellTemplate newtemp = new BellTemplate();
		//-------------------------------------Bells
		Bell newbell = new Bell(getBaseContext());
		newbell.id = debug_id++;
		newbell.name_short="Test Bell 1";
		newbell.name_long="Single bell test";
		newbell.alarm_message = "QWERTY";
		newbell.alarm_time.setToNow();
		newbell.alarm_time.minute++;
		newbell.bell_type = BellInfo.BELL_TYPE_SINGLE;
		newbell.bell_actions = BellInfo.BELL_ACTION_NOTIFY | BellInfo.BELL_ACTION_SOUND;
		newbell.icon_index = 1;
		newbell.sound_index = 1;
		newbell.bell_state = 0;
		bellHolder.add(newbell);
		
		newbell = new Bell(getBaseContext());
		newbell.id = debug_id++;
		newbell.name_short="Test Bell 2";
		newbell.name_long="Periodic bell test";
		newbell.alarm_message = "HEY HEY HEY";
		newbell.alarm_time.setToNow();
		newbell.periods_count = 3;
		newbell.period_size = newtemp.setHM(0, 1);
		newbell.alarm_time.minute += 1;
		newbell.bell_type = BellInfo.BELL_TYPE_PERIODIC;
		newbell.recalcTimepoints();
		newbell.bell_actions = BellInfo.BELL_ACTION_NOTIFY | BellInfo.BELL_ACTION_SOUND | BellInfo.BELL_ACTION_CONFIRM;
		newbell.icon_index = 1;
		newbell.sound_index = 1;
		newbell.bell_state = 0;
		bellHolder.add(newbell);
		
		//-------------------------------------Templates
		
		newtemp.name_short = "Add New";
		newtemp.icon_index = 0;
		templateHolder.add(newtemp);
		
		newtemp = new BellTemplate();
		
		newtemp.name_short = "Eyes Gym";
		newtemp.name_long = "Keep your eyes in proper state";
		newtemp.alarm_message = "Eyes Time!";
		newtemp.bell_type = BellInfo.BELL_TYPE_PERIODIC;
		newtemp.bell_actions = BellInfo.BELL_ACTION_NOTIFY | BellInfo.BELL_ACTION_SOUND;
		newtemp.periodic_alarm_start = newtemp.setHM(9,0);
		newtemp.periods_count = 20;
		newtemp.period_size = newtemp.setHM(0, 30);
		newtemp.icon_index = 1;
		newtemp.sound_index = 1;
		templateHolder.add(newtemp);
/*		
		newtemp = new BellTemplate();
		
		newtemp.name_short = "Drink Pills";
		newtemp.name_long = "1 red pill + 1 blue pill before eat";
		newtemp.alarm_message = "Time to drink pills!";
		newtemp.bell_type = BellInfo.BELL_TYPE_MULTIPLE;
		newtemp.bell_actions = BellInfo.BELL_ACTION_NOTIFY | BellInfo.BELL_ACTION_SOUND | BellInfo.BELL_ACTION_CONFIRM;
		newtemp.multiple_alarm_time = new ArrayList<Integer>();
		newtemp.multiple_alarm_time.add(new Integer(newtemp.setHM(7, 30)));
		newtemp.multiple_alarm_time.add(new Integer(newtemp.setHM(12, 30)));
		newtemp.multiple_alarm_time.add(new Integer(newtemp.setHM(19, 00)));
		newtemp.icon_index = 1;
		newtemp.sound_index = 1;
		templateHolder.add(newtemp);
*/		
		newtemp = new BellTemplate();
		
		newtemp.name_short = "Do Work";
		newtemp.name_long = "Work work work work work";
		//newtemp.alarm_message = "Earn money, you lazy b****";
		newtemp.bell_type = BellInfo.BELL_TYPE_PERIODIC;
		newtemp.bell_actions = BellInfo.BELL_ACTION_SOUND | BellInfo.BELL_ACTION_NAVAL;
		newtemp.periodic_alarm_start = newtemp.setHM(9, 0);
		newtemp.periods_count = 9;
		newtemp.period_size = newtemp.setHM(1, 0);
		newtemp.icon_index = 1;
		newtemp.sound_index = 1;
		templateHolder.add(newtemp);
		
		newtemp = new BellTemplate();
		
		newtemp.name_short = "Do Sleep";
		newtemp.name_long = "Sleep (all night)";
		newtemp.alarm_message = "KEEP CALM AND LAY DOWN";
		newtemp.bell_type = BellInfo.BELL_TYPE_SINGLE;
		newtemp.single_alarm_time = newtemp.setHM(23, 30);
		newtemp.icon_index = 1;
		newtemp.sound_index = 1;
		templateHolder.add(newtemp);
		
		newtemp = new BellTemplate();
		
		newtemp.name_short = "Buy Food";
		newtemp.name_long = "Go to the supermarket";
		newtemp.alarm_message = "Ketchup, yoghurt, chicken legs, rice, eggs, toilet paper";
		newtemp.bell_type = BellInfo.BELL_TYPE_SINGLE;
		newtemp.single_alarm_time = newtemp.setHM(18, 15);
		newtemp.icon_index = 1;
		newtemp.sound_index = 1;
		templateHolder.add(newtemp);
		
		newtemp = new BellTemplate();
		
		newtemp.name_short = "Think";
		newtemp.name_long = "Just try to think";
		newtemp.alarm_message = "What is the meaning of all?";
		newtemp.bell_type = BellInfo.BELL_TYPE_SINGLE;
		newtemp.single_alarm_time = newtemp.setHM(22, 15);
		newtemp.icon_index = 1;
		newtemp.sound_index = 1;
		templateHolder.add(newtemp);
		
		newtemp = new BellTemplate();
		
		newtemp.name_short = "Do Nothing";
		newtemp.name_long = "Sleep (all night)";
		newtemp.alarm_message = "WAKE UP!!!! Stop, no, lay down.";
		newtemp.bell_type = BellInfo.BELL_TYPE_SINGLE;
		newtemp.single_alarm_time = newtemp.setHM(4, 30);
		newtemp.icon_index = 1;
		newtemp.sound_index = 1;
		templateHolder.add(newtemp);

	}
	  
	public int onStartCommand(Intent intent, int flags, int startId) {
		//called when startService command used
		Log.d(Tag.TAG, "onStartCommand");
		/*
		bs1.alarm_message = "QWERTY";
		bs1.alarm_time.setToNow();
		//b1.alarm_time.minute++;
		bs1.alarm_time.second+=10;
		bs1.activate();
		
		bs2.alarm_message = "ASDFG";
		bs2.alarm_time.setToNow();
		bs2.alarm_time.second+=15;
		bs2.activate();
		*/
		someTask();
		return super.onStartCommand(intent, flags, startId);
	}

	public void onDestroy() {
	    super.onDestroy();
	    Log.d(Tag.TAG, "BellManager.onDestroy");
	    /*
	    bs1.deactivate();
	    bs2.deactivate();
	    */
	    
	    int cnt = bellHolder.size();
	    //TODO: try foreach
	    for (int i=0; i<cnt; i++) {
	    	bellHolder.get(i).deactivate();
	    }
	    
	    mHandler.removeCallbacks(periodicTask);
	    
	    stopForeground(true);
	    this.stopSelf();

	}

	public IBinder onBind(Intent intent) {
	    Log.d(Tag.TAG, "onBind");
	    return binder;
	}
	
	void syncUpdate() {
		Log.d(Tag.TAG, "syncUpdate");
	}
	
	void someTask() {
	}

	//TODO: use wakelocks while accessing database
	//wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");    
    //wakelock.acquire();
    //wakelock.release();
	
	//Function can be called much rarely than every minute 
	//when device is sleeping, but it's no need to check
	//frequently that time, because user would not change
	//any timers :)
	private Runnable periodicTask = new Runnable() {
		public void run() {
        	Log.v("PeriodicTimerService","Awake");
		    mHandler.postDelayed(periodicTask, ONE_MINUTE);
		}
	};
	
	class MyBinder extends Binder {
	    BellManager getService() {
	      return BellManager.this;
	    }
	  }
	
}
